package com.banshou.installs.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

import com.banshou.installs.R;
import com.banshou.installs.constant.BaseApplication;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.SettingBinding;
import com.banshou.installs.utils.CleanMessageUtil;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.SharePreUtil;
import com.banshou.installs.utils.ToastUtil;
import com.banshou.installs.utils.UpdateUtil;

import java.util.Map;

/**
 * Created by cjq on 2017/12/28.
 * Email: stoic_yb@139.com
 * features:
 */

public class SetActivity extends BaseActivity implements View.OnClickListener {
    private SettingBinding mBinding;
    private Dialog dialog;
    private String totalCacheSize;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.setting);
        initView();
    }

    private void initView() {
        try {
            totalCacheSize = CleanMessageUtil.getTotalCacheSize(this);
            mBinding.tvSize.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mBinding.tvVersion.setText(UpdateUtil.getVersionName());
        mBinding.cancel1.setOnClickListener(this);
        mBinding.cancel3.setOnClickListener(this);
        mBinding.cancel6.setOnClickListener(this);
        mBinding.next.setOnClickListener(this);

        if (mBinding.title != null) {
            mBinding.title.pagerTitle.setText(R.string.set_pass);
            mBinding.title.back.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //修改密码
            case R.id.cancel1:
                Intent intent = new Intent(this, ForgetPassActivity.class);
                intent.putExtra(Config.FROMTOPASS, Config.SET_UPDATE_PASS);
                startActivity(intent);
                break;
            //清除缓存
            case R.id.cancel3:
                if (totalCacheSize.equals("0K")) {
                    ToastUtil.toast("应用很干净，无需清理");
                    return;
                }
                CleanMessageUtil.clearAllCache(this);
                mBinding.tvSize.setText("0M");
                ToastUtil.toast("清理完毕");
                break;
            //返回
            case R.id.back:
                finish();
                break;
            case R.id.next:
                dialog = new Dialog(this, R.style.edit_AlertDialog_style);
                View view1 = LayoutInflater.from(this).inflate(R.layout.dialog3, null);
                dialog.setContentView(view1);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                dialog.show();
                RelativeLayout confirm = view1.findViewById(R.id.confirm);
                RelativeLayout cancel = view1.findViewById(R.id.cancel);
                confirm.setOnClickListener(this);
                cancel.setOnClickListener(this);
                break;
            case R.id.confirm:
                if (dialog != null) {
                    dialog.cancel();
                }
                ToastUtil.toast(Config.OUTSUCC);
                SharePreUtil.clear(BaseApplication.getContext());
                Intent intent1 = new Intent(this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.cancel:
                if (dialog != null) {
                    dialog.cancel();
                }
                break;
        }
    }

}
